var rp = require('request-promise')
var cheerio = require('cheerio')
var yzmHelper = require('../yzmHelper.js')
var globalCfg = require('../globalCfg.js')
var helper = require('../helper.js')
var config = require('./config.js')

var login13 = {
    yzm: function (req, res, cb) {

        var opts = helper.deepCopy(globalCfg.reqOpts)
        opts.encoding = 'binary'
        opts.resolveWithFullResponse = true
        opts.url = makeUrl(config.url.kaptcha + '?' + Math.floor(Math.random() * 100))
        opts.headers.Referer = makeUrl(config.url.login)
        rp.get(opts).then(function (result) {
            imgBase64 = new Buffer(result.body, 'binary').toString('base64')
            yzmHelper.decode(imgBase64, function (r) {
                var kaptcha = JSON.parse(r).pic_str
                cb(kaptcha)
            })
        }).catch(function (err) {
            res.send(err)
        })
    },
    login: function (req, res,loginErrRetry) {
        this.yzm(req, res, function (kaptcha) {
            var para = req.query
            var name = para.name
            var pass = para.password
            var opts = helper.deepCopy(globalCfg.reqOpts)
            opts.method = 'GET'
            opts.url = makeUrl(config.url.login)
            opts.headers.Referer = makeUrl(config.url.login)
            rp(opts).then(function (body) {
                var $ = cheerio.load(body)
                var d = helper.getFormData($('form#Form2').serializeArray())
                d.btnLogin = '登录'
                d.txtUserId = name
                d.txtUserPwd = pass
                d.txtCode = kaptcha
                var opts = helper.deepCopy(globalCfg.reqOpts)
                opts.formData = d
                opts.url = makeUrl(config.url.login)
                opts.headers.Referer = makeUrl(config.url.login)
                rp(opts).then(function (body) {
                    console.log(loginErrRetry)
                    if (body.indexOf("用户名密码不匹配") >= 0 || body.indexOf("验证码不正确") >= 0 || body.indexOf("用户不存在") >= 0) {
                        loginErrRetry--
                        if (loginErrRetry == 0) {
                         //   loginErrRetry = 5
                            res.json({
                                code: 0,
                                msg: '用户名或密码错误！'
                            })
                        } else {

                            login13.login(req, res,loginErrRetry);
                        }
                    } else {
                        
                        res.json({
                            code: 1,
                            msg: '验证成功！'
                        })
                    }
                }).catch(function (err) {
                    res.json({
                        code: 0,
                        msg: '错误信息：' + err
                    })
                })
            }).catch(function (err) {
                res.json({
                    code: 0,
                    msg: '错误信息：' + err
                })
            })

        })

    }
}

function makeUrl(url) {
    return config.site + url
}

module.exports = login13